/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.hiraeth.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hiraeth.common.utils.PageParams;
import com.hiraeth.common.utils.R;
import com.hiraeth.common.validator.ValidatorUtils;
import com.hiraeth.modules.sys.entity.SysDictEntity;
import com.hiraeth.modules.sys.service.SysDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("sys/dict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    @RequiresPermissions("sys:dict:page")
    public R page(@RequestParam Map<String, Object> params){
        IPage<SysDictEntity> page = sysDictService.queryPage(new PageParams(params));
        return R.ok().put("page", page);
    }

    /**
     * 查询所有字典列表
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:dict:list")
    public List<SysDictEntity> list(){
        List<SysDictEntity> list = sysDictService.list();
        SysDictEntity root = new SysDictEntity();
        root.setId(0L);
        root.setParentId(null);
        root.setName("字典根节点");
        root.setValue("字典根节点");
        list.add(root);
        return list;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:dict:info")
    public R info(@PathVariable("id") Long id){
        SysDictEntity dict = sysDictService.getById(id);
        return R.ok().put("dict", dict);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public R save(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);
        sysDictService.save(dict);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public R update(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);
        sysDictService.updateById(dict);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public R delete(@RequestBody Long[] ids){
        sysDictService.removeRecursive(ids);
        return R.ok();
    }

    /**
     * 根据字典
     * @param type
     * @return
     */
    @GetMapping("/select/{type}")
    public R getDictByType(@PathVariable("type") String type){
        QueryWrapper<SysDictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(SysDictEntity::getName,SysDictEntity::getCode,SysDictEntity::getValue)
                             .eq(SysDictEntity::getType, type)
                             .ne(SysDictEntity::getParentId, 0)
                             .orderByAsc(SysDictEntity::getOrderNum);
        List<Map<String, Object>> items = sysDictService.listMaps(queryWrapper);
        return R.ok().put("data", items);
    }

}
